package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Sound;
import com.itheima.domain.vo.ErrorResult;
import com.itheima.domain.vo.SoundVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.SoundService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class SoundManager {
    @Autowired
    private FastFileStorageClient client;
    @Autowired
    private FdfsWebServer server;


    @Reference
    private SoundService soundService;
    @Reference
    private UserInfoService userInfoService;
    //传音
    public ResponseEntity saveSound(Long userId, MultipartFile soundFile) throws IOException {
        StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize()
                , FileUtil.extName(soundFile.getOriginalFilename())
                , null);//将音频保存入FDS
        String soundUrl = server.getWebServerUrl()+storePath.getFullPath();

        //获取userInfo
        UserInfo userInfo = userInfoService.findById(userId);
        Integer age = userInfo.getAge();
        String gender = userInfo.getGender();

        //封装Sound
        Sound sound = new Sound();
        sound.setCreated(System.currentTimeMillis());
        sound.setUserId(userId);
        sound.setAge(age);
        sound.setGender(gender);
        sound.setSoundUrl(soundUrl);
        soundService.save(sound);

        return ResponseEntity.ok(null);
    }

    //收音
    public ResponseEntity findTH(Long userId) {
        SoundVo soundVo = new SoundVo();

        //查询剩余次数
        byte remTimes = soundService.findRemTimes(userId);//todo
        if(remTimes>0) {
            remTimes--;//使用一次
            soundService.useTimes(userId,remTimes);

            //查询非自己的声音库
            List<Long> sids = soundService.findSids(userId);
            if (CollUtil.isNotEmpty(sids)) {
                //随机取出sid
                long index = RandomUtil.randomLong(1, sids.size());//取一个随机数
                Long sid = sids.get((int) index);
                soundVo = soundService.findTH(sid);

                if(soundVo == null){
                    return ResponseEntity.status(500).body(ErrorResult.soundEmt());//没有声音
                }
            }else {
                return ResponseEntity.status(500).body(ErrorResult.soundEmt());//没有声音
            }
            //调用UserInfo查询头像 昵称
            UserInfo userInfo = userInfoService.findById(soundVo.getId().longValue());
            soundVo.setNickname(userInfo.getNickname());
            soundVo.setAvatar(userInfo.getAvatar());


            soundVo.setRemainingTimes(remTimes);
        }else {
            return ResponseEntity.status(500).body(ErrorResult.soundTmn());//次数用完
        }


        return ResponseEntity.ok(soundVo);
    }
}
